// 分析import
import { parse } from "es-module-lexer";
import MagicString from "magic-string";
import { prefix } from "../enum/index.js";
function RewriteImportPath(code) {
  const imports = parse(code)[0];
  const magicString = new MagicString(code);
  if (imports.length) {
    imports.forEach(({ n, s, e }) => {
      // 需要把vue换成类似于/node_modules/vue,判断是否是以. ./ /开头的
      if (!/^(\.\/|\.|\/).*/.test(n)) {
        magicString.overwrite(s, e, `${prefix}${n}`);
      }
    });
  }
  return magicString.toString();
}

export default RewriteImportPath;
